package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.MyMessageMapper;
import com.ten.mall.service.MessageService;
import com.ten.mall.vo.MyMessage;
import com.ten.mall.vo.MyMessageExample;
import com.ten.mall.vo.MyMessageExample.Criteria;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {
	@Autowired
	private MyMessageMapper messageMapper;
	@Override
	public int insertMessage(MyMessage message) {
		int i =messageMapper.insert(message);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> selectMessage(Integer start, Integer size, String name) {
		PageHelper.startPage(start, size);
		MyMessageExample example =new MyMessageExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNoneBlank(name)){
			c.andMessageTitleLike("%"+name+"%");
		}
		List<MyMessage> list =messageMapper.selectByExample(example);
		PageInfo<MyMessage> info =new PageInfo<>(list);
		Map<String, Object> map =new HashMap<>();
		map.put("list", list);
		map.put("total", info.getTotal());
		return map;
	}

	@Override
	public int updateMessage(MyMessage message) {
		int i =messageMapper.updateByPrimaryKeySelective(message);
		return i;
	}

	@Override
	public int deleteMessage(Integer id) {
		int i =messageMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public MyMessage selectById(Integer id) {
		MyMessage msg =messageMapper.selectByPrimaryKey(id);
		return msg;
	}

	@Override
	@Transactional(readOnly=true)
	public List<MyMessage> selectByState() {
		MyMessageExample example =new MyMessageExample();
		Criteria c =example.createCriteria();
		c.andMessageStateEqualTo(1);
		List<MyMessage> list =messageMapper.selectByExample(example);
		return list;
	}

}
